package com.ruicar.afs.cloud.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.product.condition.ExtrasCondition;
import com.ruicar.afs.cloud.product.entity.ExtrasInfo;
import com.ruicar.afs.cloud.product.entity.ExtrasProjectProgramInfo;
import com.ruicar.afs.cloud.product.enums.StatusEnum;
import com.ruicar.afs.cloud.product.service.ExtrasProgramConfigService;
import com.ruicar.afs.cloud.product.service.ExtrasProjectService;
import com.ruicar.afs.cloud.product.vo.ExtrasInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author xudongdong
 * @date 2020-05-18
 */
@RestController
@AllArgsConstructor
@RequestMapping("/extras")
@Api(value = "extras", description = "附加贷项目信息")
public class ExtrasProjectController {

    private final ExtrasProjectService extrasService;

    private final ExtrasProgramConfigService extrasProgramConfigService;

    @PostMapping("/getExtrasList")
    @ApiOperation(value = "多条件分页获取附加贷项目数据")
    public IResponse<IPage<ExtrasInfo>> getExtrasList(@RequestBody QueryCondition<ExtrasCondition> extras) {

        Page page = new Page(extras.getPageNumber(), extras.getPageSize());
        IPage<List<ExtrasInfo>> list = extrasService.getProjectDataList(page, extras.getCondition());
        return IResponse.success(list);
    }

    @PostMapping("/getProjectData")
    @ApiOperation(value = "获取附加项目数据排除已经添加到方案的")
    public IResponse<IPage<ExtrasInfo>> getProjectData(@RequestBody ExtrasCondition extras) {
        Page page = new Page(extras.getPageNumber(), extras.getPageSize());
        IPage<List<ExtrasInfo>> list = extrasService.getProjectList(page, extras);
        return IResponse.success(list);
    }

    @Transactional
    @PostMapping("/addExtras")
    @ApiOperation(value = "保存/修改附加贷项目数据")
    public IResponse saveExtras(@Valid @RequestBody ExtrasInfo extras) {
        if (AfsEnumUtil.key(StatusEnum.NO).equals(extras.getIsExistsFather())) {
            extras.setFatherNo("0");//专用于层级列表使用
        }

        int numExtrasProjectNo = extrasService.count(Wrappers.<ExtrasInfo>query().lambda()
                .eq(ExtrasInfo::getExtrasProjectNo, extras.getExtrasProjectNo())
        );
        if((numExtrasProjectNo>1 && extras.getId()!=null)||(numExtrasProjectNo>0 && extras.getId()==null)){
            return IResponse.fail("附加项目编号："+extras.getExtrasProjectNo() + "已存在");
        }

        int numExtrasProjectName = extrasService.count(Wrappers.<ExtrasInfo>query().lambda()
                .eq(ExtrasInfo::getExtrasProjectName, extras.getExtrasProjectName())
        );
        if((numExtrasProjectName>1 && extras.getId()!=null)||(numExtrasProjectName>0 && extras.getId()==null)){
            return IResponse.fail("附加项目名称："+extras.getExtrasProjectName() + "已存在");
        }

        extrasService.saveOrUpdate(extras);
        return IResponse.success(Boolean.TRUE).setMsg("操作成功");
    }

    @Transactional
    @PostMapping(value = "/enableExtras/{ids}")
    @ApiOperation(value = "批量启用附加贷项目")
    public IResponse<Boolean> enableByIds(@PathVariable List<Long> ids) {
        for (Long id : ids) {
            ExtrasInfo extrasInfo = extrasService.getById(id);
            if (extrasInfo != null && !AfsEnumUtil.key(StatusEnum.YES).equals(extrasInfo.getIsEnable())) {
                extrasInfo.setIsEnable(AfsEnumUtil.key(StatusEnum.YES));
                extrasService.updateById(extrasInfo);
            }
        }
        return new IResponse<Boolean>().setMsg("操作成功");
    }

    @Transactional
    @PostMapping(value = "/disableExtras/{ids}")
    @ApiOperation(value = "批量禁用附加贷项目")
    public IResponse<Boolean> disableByIds(@PathVariable List<Long> ids) {
        for (Long id : ids) {
            ExtrasInfo extrasInfo = extrasService.getById(id);
            if (extrasInfo != null && !AfsEnumUtil.key(StatusEnum.NO).equals(extrasInfo.getIsEnable())) {
                extrasInfo.setIsEnable(AfsEnumUtil.key(StatusEnum.NO));
                extrasService.updateById(extrasInfo);
            }
        }
        return new IResponse<Boolean>().setMsg("操作成功");
    }

    @PostMapping("/getSelectFatherNo")
    @ApiOperation(value = "获取父类编号")
    public IResponse<IPage<ExtrasInfoVo>> getSelectFatherNo(@RequestBody ExtrasCondition condition) {
        Page page = new Page(condition.getPageNumber(), condition.getPageSize());
        IPage<List<ExtrasInfoVo>> list = extrasService.getSelectFatherNo(page);
        return IResponse.success(list);
    }

    @RequestMapping(value = "/deleteExtras/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "通过id删除")
    @Transactional
    @SysLog("删除附加贷项目信息")
    public IResponse<Boolean> delById(@PathVariable String id) {
        ExtrasInfo extrasInfo = extrasService.getById(id);
        if (AfsEnumUtil.key(StatusEnum.YES).equals(extrasInfo.getIsEnable())) {
            return IResponse.fail("生效方案不可删除！");
        } else {
            /**
             * 第一步：删除附加贷方案项目信息
             */
            extrasProgramConfigService.remove(Wrappers.<ExtrasProjectProgramInfo>query().lambda()
                    .eq(ExtrasProjectProgramInfo::getExtrasProjectId, id));
            /**
             * 第二步：删除附加贷项目信息
             */
            extrasService.removeById(id);
            return new IResponse<Boolean>().setMsg("删除成功");
        }
    }

}
